/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.funmodelling;

import java.io.*;
import java.util.*;

/**
 *
 * @author Mustafa Tosun
 */
public class DataLoader {

    public static VModel LoadFromFile(String file1, String file2) {

        ArrayList<Slice> slices = new ArrayList<Slice>();
        ArrayList<Vertex> vList = new ArrayList<Vertex>();
        ArrayList<Vertex> mList = new ArrayList<Vertex>();
        Stack<Integer> sliceIdStck = new Stack<Integer>();
        Scanner s1, s2;
        String buff;
        int sliceId = Integer.MIN_VALUE;
        int sliceIdH = Integer.MIN_VALUE;
        float x, y, z;
        int pos1, pos2;
        Vertex v;
        int i = 0;

        try {

            buff = "";
            sliceId = Integer.MIN_VALUE;


            s2 = new Scanner(new BufferedReader(new FileReader(file2)));
            while (s2.hasNext()) {
                buff = s2.next();
                if (buff.length() == 0) {
                    continue;
                }
                if (buff.startsWith("#")) {
                    buff = buff.substring(1, buff.length());
                    sliceId = Integer.parseInt(buff);
                    sliceIdStck.push(sliceId);
                } else {
                    pos1 = buff.indexOf(":");
                    pos2 = buff.lastIndexOf(":");

                    x = Float.parseFloat(buff.substring(0, pos1));
                    z = Float.parseFloat(buff.substring(pos1 + 1, pos2));
                    y = Float.parseFloat(buff.substring(pos2 + 1, buff.length()));
                    v = new Vertex(x, y, z);
                    v.Id = sliceIdStck.pop();
                    mList.add(v);
                }
            }
            s2.close();

            if(!sliceIdStck.isEmpty())
            {
                    v = new Vertex();
                    v.Id = sliceIdStck.pop();
                    mList.add(v);
            }

            buff = "";
            sliceId = Integer.MIN_VALUE;

            s1 = new Scanner(new BufferedReader(new FileReader(file1)));
            while (s1.hasNext()) {
                //read line
                buff = s1.next();
                // if empty line continue
                if (buff.length() == 0) {
                    continue;
                }
                //slice identifier line
                if (buff.startsWith("#")) {
                    buff = buff.substring(1, buff.length());
                    sliceId = Integer.parseInt(buff);
                    if (i > 0) {
                        sliceIdH = sliceIdStck.pop();
                         //v = new Vertex(0.66f,-0.23f,5.1701f);
                        v = new Vertex(sliceIdH);
                         //v.Id = sliceIdH;
                        //new slice creates with vertex list
                         int mk = mList.indexOf(v);
                         v = mList.get(mk);

                        slices.add(new Slice(vList, v , sliceIdH));
                        //reset vertex list for next slice
                        vList = new ArrayList<Vertex>();
                    }
                    //save slice identity
                    sliceIdStck.push(sliceId);
                    i++;
                } else {
                    //parse vertex coords
                    pos1 = buff.indexOf(":");
                    pos2 = buff.lastIndexOf(":");

                    x = Float.parseFloat(buff.substring(0, pos1));
                    z = Float.parseFloat(buff.substring(pos1 + 1, pos2));
                    y = Float.parseFloat(buff.substring(pos2 + 1, buff.length()));
                    //create new vertex and add to list
                    v = new Vertex(x, y, z);
                    vList.add(v);
                }
            }
            //for the last slice
            sliceIdH = sliceIdStck.pop();
            v = new Vertex(sliceIdH);
             //v.Id = sliceIdH;
            //new slice creates with vertex list
             int mk = mList.indexOf(v);
             v = mList.get(mk);

            slices.add(new Slice(vList, v , sliceIdH));
            vList = new ArrayList<Vertex>();
            //close scanner
            s1.close();





        } catch (IOException e) {
            System.err.println(e.getMessage());
        }

        VModel model = new VModel(slices);
        return model;

    }
}
